<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta charset="UTF-8">
<title>修改套餐</title>

<link rel="stylesheet" href="layui/css/layui.css">
<script type="text/javascript" src="js/jquery-1.8.3.js"></script>
<script src="layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 --> 

</head>
<body>
<center><br><h2>套餐信息</h2><br><br></center>

<form class="layui-form" action="">
  <!-- <div class="layui-form-item">
    <label class="layui-form-label">长输入框</label>
    <div class="layui-input-block">
      <input type="text" name="title" required lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">短输入框</label>
    <div class="layui-input-inline">
      <input type="text" name="username" required lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
    </div>
  </div> -->
  <div class="layui-form-item">
    <label class="layui-form-label">套餐编号</label>
    <div class="layui-input-inline">
      <input type="text" name="pgid" readonly value="" lay-verify="required" placeholder="请输入套餐编号 [确保唯一]" autocomplete="off" class="layui-input">
    </div>
    <label class="layui-form-label">套餐名称</label>
    <div class="layui-input-inline">
      <input type="text" name="pgname" required value="" lay-verify="required" placeholder="请输入套餐名称" autocomplete="off" class="layui-input">
    </div>
    <label class="layui-form-label">套餐类型</label>
    <div class="layui-input-inline">
      <select name="pgtype" readonly lay-verify="required">
        <option value=""></option>
        <option value="1">报单购货</option>
        <option value="2" selected>套餐购货</option>
        <option value="3">单品购货</option>
        <option value="4">组合购货</option>
      </select>
    </div>
  </div>
  <!-- <div class="layui-form-item">
    <label class="layui-form-label">添加商品</label>
    <div class="layui-input-block">
    	<table class="layui-hide" name="packageitems" id="test" lay-filter="test"></table>
    </div>
  </div> -->
  <div class="layui-form-item">
  	<label class="layui-form-label">市场价</label>
    <div class="layui-input-inline">
      <input type="text" name="marketprice" required value="" lay-verify="required|number" placeholder="请输入市场价" autocomplete="off" class="layui-input">
    </div>
    <label class="layui-form-label">优惠价</label>
    <div class="layui-input-inline">
      <input type="text" name="price" required value="" lay-verify="required|number" placeholder="请输入优惠价" autocomplete="off" class="layui-input">
    </div>
  	<label class="layui-form-label">库存量</label>
    <div class="layui-input-inline">
      <input type="text" name="number" required value="" lay-verify="required|number" placeholder="请输入套餐余量" autocomplete="off" class="layui-input">
    </div>
  </div>
  <!-- <div class="layui-form-item">
    <label class="layui-form-label">密码框</label>
    <div class="layui-input-inline">
      <input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
    </div>
    <div class="layui-form-mid layui-word-aux">辅助文字</div>
  </div> -->
  <!-- <div class="layui-form-item">
    <label class="layui-form-label">复选框</label>
    <div class="layui-input-block">
      <input type="checkbox" name="like[write]" title="写作">
      <input type="checkbox" name="like[read]" title="阅读" checked>
      <input type="checkbox" name="like[dai]" title="发呆">
    </div>
  </div> -->
  <div class="layui-form-item">
    <label class="layui-form-label">上架</label>
    <div class="layui-input-block">
      <input type="radio" name="state" id="onsale" value="1" title="上架" checked>
      <input type="radio" name="state" id="offsale" value="0" title="下架" >
    </div>
  </div>
  <!-- <div class="layui-form-item">
    <label class="layui-form-label">上架</label>
    <div class="layui-input-block">
      <input type="checkbox" name="switch" lay-skin="switch">
      设置value="1"可自定义值，否则选中时返回的就是默认的on lay-text="是|否"
      <input type="checkbox" name="state" lay-skin="switch" checked value="1" lay-text="是|否">
    </div>
  </div> -->
  <div class="layui-form-item layui-form-text">
    <label class="layui-form-label">套餐说明</label>
    <div class="layui-input-block">
      <textarea name="introduction" value="" placeholder="请输入内容" class="layui-textarea"></textarea>
    </div>
  </div>
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" id="submitbut" lay-submit lay-filter="formDemo">更新套餐</button>
      <button class="layui-btn" id="updatebut" lay-submit lay-filter="formDemo2">更新商品</button>
      <button class="layui-btn" id="showbut" lay-submit lay-filter="formDemo3">查看商品</button>
      <button type="reset" id="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
</form>


<script>
$(function(){
	var pgid='<%=request.getParameter("pgid") %>';
	$.post("<%=basePath%>getpackagebypgid.do",{pgid:pgid },function(res){
		$("input[name='pgid']").val(res.data.pgid);
		$("input[name='pgname']").val(res.data.pgname);
		$("input[name='marketprice']").val(res.data.marketprice);
		$("input[name='price']").val(res.data.price);
		$("input[name='number']").val(res.data.number);
		var state=res.data.state;
		if(state==0){
			$("#onsale").removeAttr("checked");
			$("#offsale").attr('checked',true);
		} 
		//alert(res.data.specification);
		$("textarea[name='introduction']").val(res.data.introduction);
	});
	
});


//创建实例  要想数据初始化 这里必须要写
layui.use(['table', 'layer', 'form'], function () {
	var table = layui.table;
	layer = layui.layer;
	form = layui.form;

	
  //监听提交
  form.on('submit(formDemo)', function(data){
	var param=data.field;//定义临时变量获取表单提交过来的数据，非json格式
	var data=JSON.stringify(param);//表格数据序列化
	//console.log(data);//测试是否获取到表单数据，调试模式下在页面控制台查看
	//layer.msg(data);
    
    $.ajax({
		url:"<%=basePath%>updatepackage.do",//接口
        type:'post',//method请求方式，get或者post
        dataType:'json',//预期服务器返回的数据类型
        data:JSON.stringify(param),//表格数据序列化
        contentType: "application/json; charset=utf-8",
        success: function(res){//res为相应体,function为回调函数
        	if (res.code == 1) {
				layer.msg(res.msg, { icon: 1, time: 1500 });
	            //layer.alert(res.msg, {icon:1});
	            //$("#reset").click();//调用重置按钮将表单数据清空
	            //layer.close(index);  //关闭弹窗
			} else{
				layer.msg(res.msg, { icon: 2, time: 1500 });
				//layer.alert(res.msg, {icon: 5});
			}
        },
        error: function(){
        	layer.msg("操作失败", { icon: 2 });
        	//layer.alert('操作失败', {icon:5});
        }
	}); 
	return false;//阻止表单跳转。如果需要表单跳转,去掉这段即可，但会影响弹框效果。
  });
  
  form.on('submit(formDemo2)', function(data){
		var param=data.field;//定义临时变量获取表单提交过来的数据，非json格式
		//var data=JSON.stringify(param);//表格数据序列化
		//console.log(data);//测试是否获取到表单数据，调试模式下在页面控制台查看
		//layer.msg(data);
		
		//套餐里添加商品
		//先关闭当前弹出层
		var index = parent.layer.getFrameIndex(window.name);
		parent.layer.close(index);//关闭当前页
		//window.parent.location.replace(location.href)//刷新父级页面
		//打开新的弹出层
		parent.layer.open({
            type: 2,
            title: '添加商品',
            skin: 'layui-layer-molv',
            shadeClose: false,
            shade: 0.6,
            area: ['60%', '80%'],
            maxmin: true,
            content: '<%=basePath%>/m_admin/updateproducttopackage.jsp?pgid='+param.pgid, //跳转的页面
            cancel: function (index) {
                $(".layui-laypage-btn").click();//这里用于关闭Open时触发回调函数  刷新父页面数据  一定要引入Jquery
            }
      	});

  });
  
  form.on('submit(formDemo3)', function(data){
		var param=data.field;//定义临时变量获取表单提交过来的数据，非json格式
		//var data=JSON.stringify(param);//表格数据序列化
		//console.log(data);//测试是否获取到表单数据，调试模式下在页面控制台查看
		//layer.msg(data);
		
		//套餐里添加商品
		//先关闭当前弹出层
		var index = parent.layer.getFrameIndex(window.name);
		parent.layer.close(index);//关闭当前页
		//window.parent.location.replace(location.href)//刷新父级页面
		//打开新的弹出层
		parent.layer.open({
          type: 2,
          title: '查看商品',
          skin: 'layui-layer-molv',
          shadeClose: false,
          shade: 0.6,
          area: ['60%', '80%'],
          maxmin: true,
          content: '<%=basePath%>/m_admin/getproductinpackage.jsp?pgid='+param.pgid, //跳转的页面
          cancel: function (index) {
              $(".layui-laypage-btn").click();//这里用于关闭Open时触发回调函数  刷新父页面数据  一定要引入Jquery
          }
    	});
	});

});
</script>

</body>
</html>